/**
 * @Package: 	com.group.DAO 
 * @author: 	Liu  
 * @date: 		Dec 27, 2018 10:24:00 AM 
 */
package com.group.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.group.C3P0Utils.C3P0Utils;
import com.group.pojo.GUser;

/**
 * @ClassName:	 GUserDAO.java
 * @Description: 用户数据处理DAO
 * @author:		 Liu
 * @date: 		 Dec 27, 2018 10:24:00 AM 
 */

public class GUserDAO {
	
	/**
	 * @Function: 	 GUserDAO.java
	 * @Description: 用户登录
	 * @param:		 ID，密码
	 * @return:		   存在账号(true)，不存在账号(false)
	 * @author: 	 Liu
	 * @date:		 Dec 27, 2018 10:41:43 AM 
	 */
	public boolean login(String userID, String password) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM g_user WHERE user_id = ? AND user_pwd = ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userID);
			pstmt.setString(2, password);
			rs = pstmt.executeQuery();
			//存在账号
			if(rs.next())	return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
		return false;
	}
	
	/**
	 * @Function: 	 GUserDAO.java
	 * @Description: 获取用户名
	 * @param:		  用户类
	 * @return:		   用户名
	 * @author: 	 Liu
	 * @date:		 Jan 2, 2019 11:46:08 AM 
	 */
	public String getNameByID(GUser user) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String name = null;
		String sql = null;
		String userID = user.getUserID();
		if(null != user.isTeacher(userID))
			sql = "SELECT * FROM g_user, g_teacher WHERE user_id = teacher_id AND user_id = ?";
		else
			sql = "SELECT * FROM g_user, g_student WHERE user_id = student_id AND user_id = ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, userID);
			rs = pstmt.executeQuery();
			//存在账号
			if(rs.next()) {
				if(null != user.isTeacher(userID)) name = rs.getString("teacher_name");
				else name = rs.getString("student_name"); 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
		return name;
	}
	
	/**
	 * @Function: 	 GUserDAO.java
	 * @Description: 该函数的功能描述
	 * @param:		   参数
	 * @return:		   返回结果描述
	 * @author: 	 Miss Zheng
	 * @date:		 2019年1月3日 下午8:31:12 
	 */
	public void updatePwd(String userID, String newPwd) {
		// TODO Auto-generated method stub
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "UPDATE g_user SET user_pwd = ? WHERE user_id = ?";
		try {
			conn = C3P0Utils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, newPwd);
			pstmt.setString(2, userID);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			C3P0Utils.close(rs, pstmt, conn);
		}
	}
}
